//
//  TopicListViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "TopicListViewController.h"

@interface TopicListViewController ()<UIWebViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UIWebView *webView;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,strong)UILabel *label;
@end

@implementation TopicListViewController

- (UILabel *)label{
    if (!_label) {
        _label=[[UILabel alloc]initWithFrame:CGRectMake(0, kHeight/2, kWidth, 30)];
        _label.backgroundColor=[UIColor cyanColor];
        _label.text=@"评论";
        _label.textAlignment=NSTextAlignmentCenter;
        _label.textColor=[UIColor purpleColor];
    }
    return _label;
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}


- (UIWebView *)webView{
    if (!_webView) {
        _webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 100)];
        _webView.delegate=self;
        _webView.scrollView.bounces=NO;
        _webView.scrollView.bouncesZoom=NO;
    }
    return _webView;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
        _tableView.dataSource=self;
        _tableView.delegate=self;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //[self.view addSubview:self.webView];
   // [self.view addSubview:self.label];
    self.tableView.tableHeaderView=self.webView;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicListCell" bundle:nil] forCellReuseIdentifier:@"TopicList_cell"];
    //上拉加载
    self.tableView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];

    [RequestManager fetchDataWithUrl:kTopicListUrl parameter:@{@"contentid":self.contentID} method:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
       // NSLog(@"%@",bigDic);
        //NSLog(@"%@",bigDic[@"data"][@"postsinfo"][@"html"]);
        //NSLog(@"%@",bigDic[@"data"][@"commentlist"]);
        [self.webView loadHTMLString:bigDic[@"data"][@"postsinfo"][@"html"] baseURL:nil];
        for (NSDictionary *smallDic in bigDic[@"data"][@"commentlist"]) {
            TopicModel *model=[[TopicModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }
    } updateUI:^{
    
        
        [self.tableView reloadData];
    }];
    
    
    
  
    
    }

//上拉加载
- (void)loadMoreData{
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshing];
    
}

#pragma mark--------UIWebView的协议

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    NSString *js = @"function imgAutoFit() { \
    var imgs = document.getElementsByTagName('img'); \
    for (var i = 0; i < imgs.length; ++i) {\
    var img = imgs[i];   \
    img.style.maxWidth = %f;   \
    } \
    }";
    js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
    [webView stringByEvaluatingJavaScriptFromString:js];
    [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];
    
    //获取到webview的高度
    CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    self.webView.frame = CGRectMake(self.webView.frame.origin.x,self.webView.frame.origin.y, kWidth, height+20);
    
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
}




#pragma mark---------------UITableView的协议-------------------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 100;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TopicListCell *cell=[tableView dequeueReusableCellWithIdentifier:@"TopicList_cell" forIndexPath:indexPath];
    [cell setCellWithModel:self.dataSource[indexPath.row]];
  
    
    return cell;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end





